package emergency;

import java.util.ArrayList;

import unit.Unit;

public class EmergencyPolicyASAP extends EmergencyPolicy {

	/**
	 * Abstract method for sorting units, depending on which concrete emergency
	 * policy is used
	 * 
	 * @param units
	 *            , all the units
	 * 
	 * @param e
	 *            , the emergency you want to base the sorting on ( the location
	 *            is important )
	 * 
	 * @return the list with units, sorted based on the fastest on the emergency
	 *         location
	 */
	@Override
	public ArrayList<Unit> sortUnits(ArrayList<Unit> units, Emergency e) {
		ArrayList<Unit> result = new ArrayList<Unit>();
		Unit fastest = null;

		while (units.size() > 0) {
			double fastestMin = 00.00;
			int indexToRemove = -1;
			for (Unit u : units) {
				double distance = e.getLocation().distance(
						u.getCurrentLocation());
				double amountOfMinutes = 60.00;
				double distancePerMin = u.getSpeed() / amountOfMinutes;
				double minCurrentUnit = (distancePerMin * distance);
				if (minCurrentUnit > fastestMin) {
					fastest = u;
					fastestMin = minCurrentUnit;
					indexToRemove = units.indexOf(u);
				}
			}
			result.add(fastest);

			units.remove(indexToRemove);
		}
		return result;
	}

	/**
	 * Method for getting a string representation of this emergency policy
	 * 
	 * @return Returns a string representation of this emergency policy
	 */
	@Override
	public String toString() {
		return "ASAP Emergencypolicy";
	}
}
